/**
 * Created by Lee on 2017/9/4.
 */


var background = '#ab7947';
var piecesColor = '#666';
var arr1 = [0, 0, 0, 0];
var arr2 = [0, 0, 0, 0];
var arr3 = [0, 0, 0, 0];
var arr4 = [0, 0, 0, 0];
var arr = [arr1, arr2, arr3, arr4];


var saveArray = new Array(4);
for(var m = 0;m < 4; m++){
    saveArray[m] = new Array(4);
}

var puzzleDiv = document.getElementsByClassName('puzzle')[0];
var pieces = puzzleDiv.getElementsByClassName('pieces');


ini();
var undo_btn = document.getElementById('undo');
undo_btn.onclick = function () {
    undo();
};
document.onkeyup = function (event) {

    move(event);

}
;


/**
 * 初始化，生成2格 2:4=3:1
 */
function ini() {
    var count = 0;
    var temp = 0;
    while (count < 2) {
        var random = randomInt(0, 15);  //对应16个格子
        if (random !== temp) {
            temp = random;
            var show = randomInt(1, 16);
            if (show >= 12) {
                show = 4;
            } else {
                show = 2;
            }
            pieces[temp].innerHTML = show;
            pieces[temp].style.backgroundColor = piecesColor;

            var i = Math.floor(temp / 4);
            var j = temp % 4;
            arr[i][j] = show;

            count++;
        }
    }
    changeTo(saveArray,arr);
}

function move(event) {



    var temp = 0;
    var flag, change;
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if (e) {
        if (e.keyCode == 13) {
            undo();
        }
        if (e.keyCode == 37) { // 按 左方向键
            changeTo(saveArray,arr);
            change = 0;
            for (var i in arr) {
                flag = 1;
                while (flag == 1) {
                    //移位
                    flag = 0;
                    for (var j = 1; j <= 3; j++) {
                        if (arr[i][j] != 0) {
                            if (arr[i][j - 1] == 0) {
                                temp = arr[i][j];
                                arr[i][j] = arr[i][j - 1];
                                arr[i][j - 1] = temp;
                                flag = 1;
                                change = 1;
                            }
                        }
                    }
                    //合并
                    for (var j = 0; j < 3; j++) {
                        if (arr[i][j] == 0) {
                            break;
                        } else if (arr[i][j] == arr[i][j + 1]) {
                            arr[i][j] *= 2;
                            arr[i][j + 1] = 1;
                            change = 1;
                        }
                    }
                }
                //再移位
                for (var j = 1; j < 3; j++) {
                    if (arr[i][j] == 0) {
                        break;
                    } else if (arr[i][j] == 1) {
                        temp = arr[i][j];
                        arr[i][j] = arr[i][j + 1];
                        arr[i][j + 1] = temp;
                    }
                }
            }
        }
        if (e.keyCode == 38) { // 按 上方向键
            changeTo(saveArray,arr);
            change = 0;
            for (var j in arr) {
                flag = 1;
                while (flag == 1) {
                    //移位
                    flag = 0;
                    for (var i = 1; i <= 3; i++) {
                        if (arr[i][j] != 0) {
                            if (arr[i - 1][j] == 0) {
                                temp = arr[i][j];
                                arr[i][j] = arr[i - 1][j];
                                arr[i - 1][j] = temp;
                                flag = 1;
                                change = 1;
                            }
                        }
                    }
                    //合并
                    for (var i = 0; i < 3; i++) {
                        if (arr[i][j] == 0) {
                            break;
                        } else if (arr[i][j] == arr[i + 1][j]) {
                            arr[i][j] *= 2;
                            arr[i + 1][j] = 1;
                            change = 1;
                        }
                    }
                }
                //再移位
                for (var i = 1; i < 3; i++) {
                    if (arr[i][j] == 0) {
                        break;
                    } else if (arr[i][j] == 1) {
                        temp = arr[i][j];
                        arr[i][j] = arr[i + 1][j];
                        arr[i + 1][j] = temp;
                    }
                }
            }
        }
        if (e.keyCode == 39) { // 按 右方向键
            changeTo(saveArray,arr);
            change = 0;
            for (var i in arr) {
                flag = 1;
                while (flag == 1) {
                    //移位
                    flag = 0;
                    for (var j = 2; j >= 0; j--) {
                        if (arr[i][j] != 0) {
                            if (arr[i][j + 1] == 0) {
                                temp = arr[i][j];
                                arr[i][j] = arr[i][j + 1];
                                arr[i][j + 1] = temp;
                                flag = 1;
                                change = 1;
                            }
                        }
                    }
                    //合并
                    for (var j = 3; j > 0; j--) {
                        if (arr[i][j] == 0) {
                            break;
                        } else if (arr[i][j] == arr[i][j - 1]) {
                            arr[i][j] *= 2;
                            arr[i][j - 1] = 1;
                            change = 1;
                        }
                    }
                }
                //再移位
                for (var j = 2; j > 0; j--) {
                    if (arr[i][j] == 0) {
                        break;
                    } else if (arr[i][j] == 1) {
                        temp = arr[i][j];
                        arr[i][j] = arr[i][j - 1];
                        arr[i][j - 1] = temp;
                    }
                }
            }
        }
        if (e.keyCode == 40) { // 按 下方向键
            changeTo(saveArray,arr);
            change = 0;
            for (var j in arr) {
                flag = 1;
                while (flag == 1) {
                    //移位
                    flag = 0;
                    for (var i = 2; i >= 0; i--) {
                        if (arr[i][j] != 0) {
                            if (arr[i + 1][j] == 0) {
                                temp = arr[i][j];
                                arr[i][j] = arr[i + 1][j];
                                arr[i + 1][j] = temp;
                                flag = 1;
                                change = 1;
                            }
                        }
                    }
                }
                //合并
                for (var i = 3; i > 0; i--) {
                    if (arr[i][j] == 0) {
                        break;
                    } else if (arr[i][j] == arr[i - 1][j]) {
                        arr[i][j] *= 2;
                        arr[i - 1][j] = 1;
                        change = 1;
                    }
                }
            }
            //再移位
            for (var i = 2; i > 0; i--) {
                if (arr[i][j] == 0) {
                    break;
                } else if (arr[i][j] == 1) {
                    temp = arr[i][j];
                    arr[i][j] = arr[i - 1][j];
                    arr[i - 1][j] = temp;
                }
            }
        }

        //console.log(arr);
        if (change == 1) {
            show();
            setTimeout(addOne,130);
            //addOne();
            //show();
        }
    }
}


function addOne(flag) {
    flag = flag?flag:0;//判断是否还有位置可加
    if(flag == 0) {
        for (var m in arr) {
            for (var n in arr) {
                if (arr[m][n] == 0) {
                    flag = 1;
                }
            }
        }
    }
    if(flag == 1) {
        var random = randomInt(0, 15);
        var i = Math.floor(random / 4);
        var j = random % 4;
        if (arr[i][j] != 0) {
            addOne(1);
        } else {
            var show = randomInt(1, 16);
            if (show >= 12) {
                show = 4;
            } else {
                show = 2;
            }
            pieces[random].innerHTML = show;
            pieces[random].style.backgroundColor = piecesColor;
            arr[i][j] = show;

        }
    }
}

function randomInt(start, end) {
    end += 1;
    return Math.floor(Math.random() * (end - start)) + start;
}

function show() {
    for (var i in arr) {
        for (var j in arr) {
            var key = parseInt(i * 4) + parseInt(j);
            if (arr[i][j] != 0 && arr[i][j] != 1) {
                pieces[key].innerHTML = arr[i][j];
                pieces[key].style.backgroundColor = piecesColor;
            } else {
                arr[i][j] = 0;
                pieces[key].innerHTML = '';
                pieces[key].style.backgroundColor = background;
            }
        }
    }
}

function changeTo(change, target){
    for(var m in target){
        for(var n in target){
            change[m][n] = target[m][n];
        }
    }
}

function undo() {
    var con = confirm();
    console.log(con);
    if(con){
        changeTo(arr,saveArray);
        show();
    }else{
        alert('wrong password!');
    }

}
var password = ['第一个密码','第二个密码','第三个密码'];
function confirm(){
    var psw_input = document.getElementById('psw').value;
    for(var i in password){
        if(psw_input == password[i]){
            password.push('万能密码');
            password.shift();
            return true;
        }
    }
    return false;
}